package com.ruoyi.law.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.law.domain.dto.LawAuditDTO;
import com.ruoyi.law.domain.entity.LawLawyer;
import com.ruoyi.law.domain.entity.LawTeam;
import com.ruoyi.law.enums.AuditState;
import com.ruoyi.law.enums.UserType;
import com.ruoyi.law.mapper.LawLawyerMapper;
import com.ruoyi.law.service.ILawLawyerService;
import com.ruoyi.law.service.ILawTeamService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 律师申请表 服务实现类
 * </p>
 *
 * @author code
 * @since 2022-11-30
 */
@Service
public class LawLawyerServiceImpl extends ServiceImpl<LawLawyerMapper, LawLawyer> implements ILawLawyerService {

    @Autowired
    ILawTeamService teamService;
    @Autowired
    ISysUserService userService;

    @Override
    public void audit(LawAuditDTO auditDTO) {
        LawLawyer lawLawyer = super.getById(auditDTO.getId());
        String state = auditDTO.getState();
        lawLawyer.setState(state);
        super.updateById(lawLawyer);

        Long userId = lawLawyer.getUserId();
        if (AuditState.approved.getValue().equals(state)) {
            // 审核通过后写入团队表
            LawTeam lawTeam = new LawTeam()
                    .setName(lawLawyer.getName())
                    .setUserId(userId)
                    .setLawyarId(lawLawyer.getId())
                    .setPic(lawLawyer.getProfessionalPhoto())
                    .setInfo(lawLawyer.getInfo())
                    .setPhone(lawLawyer.getPhone())
                    .setCompany(lawLawyer.getCompany());
            teamService.save(lawTeam);

            // 修改userType为律师
            SysUser sysUser = userService.selectUserById(userId);
            sysUser.setUserType(UserType.lawyer.getType());
            userService.updateUser(sysUser);
        }
    }
}
